import 'package:flutter/material.dart';
import 'package:github/server.dart' as github;
import 'package:ok_github/repo/message_repo.dart';
import 'package:ok_github/utils/date_utils.dart';
import 'package:ok_ui/flutter_ok_ui.dart';

class NotificationPage extends StatefulWidget {
  @override
  _NotificationPageState createState() => _NotificationPageState();
}

class _NotificationPageState extends State<NotificationPage> {
  NotificationRepo _repo = NotificationRepo();

  @override
  void initState() {
    super.initState();
    _repo.refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: OKAppBar(
        title: "我的通知",
      ),
      body: StreamBuilder<List<github.Notification>>(
        stream: _repo.notificationStream,
        initialData: _repo.notification,
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Container();
          }
          var data = snapshot.data;
          return ListView.builder(
            itemBuilder: _buildItem,
            itemCount: data.length,
          );
        },
      ),
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    var data = _repo.notification[index];

    return ListTile(
      title: Text(data.repository.name),
      subtitle: Text(data.subject.title),
      trailing: Text(DateUtils.format(data.updatedAt)),
    );
  }
}
